#include "lv_drv_conf.h"
#include "lvgl/lvgl.h"
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "stdio.h"
#include "semaphore.h"
#include "hx711.h"

#define thred_hx711 30


void uln2003(int step);

pthread_t thread;
pthread_t thread_ttt;
sem_t sem;
int control_fig=0; // "1" to contorl  "0" not cintorl
int export=0;
float hx711_value;
/***************************child thread*************************/
void *thread_fuction(void *arg) {
    float value;
    static float value_last;
	while(1){
	value=hx711_readCount();
	if((value >=0)&&(value<1000)){ 
        if((value_last-value <100)||(value_last -value >-100))
            hx711_value=value;
       }
	//printf("%f\n",hx711_value);
    }
    	value_last=value;
}

void *thread_step(void *arg){
if(control_fig==1){      //contorl mode
        if(hx711_value < export ) {
                for(int i=1;i<5;i++){
                 uln2003(i);
                }
        }       
        else{
                uln2003(5);
        }
    }
    else{                    //not contorl mode
        uln2003(5);
    }
}

lv_obj_t *tileview;
lv_style_t  style;

lv_obj_t* arc;

lv_obj_t* round_button;
lv_style_t  round_style;

lv_obj_t* start_button;
lv_style_t  start_style;

lv_obj_t* small_button;

lv_obj_t* exti_button;
lv_obj_t * exti_label;
lv_obj_t * label;
lv_obj_t * exti_label;


lv_timer_t * timer;
lv_timer_t * contorl_timer;
static void arc_event_handler(lv_event_t * e);
static void button_event_handler(lv_event_t * e);
static void exti_event_handler(lv_event_t * e);

static void my_timer(lv_timer_t * timer);
static void my_contorl_timer(lv_timer_t * timer);;

char str_lcd[10];
void app_UI_init(void)
{
    tileview = lv_obj_create(lv_scr_act());
    lv_obj_set_size(tileview, 1024, 600);//设置覆盖大小
/******************************背景****************************************/
    lv_style_init(&style);
    lv_style_set_bg_color( &style, lv_color_hex(0x000000));//背景色 黑色
    lv_style_set_bg_grad_color(&style, lv_color_hex(0x696969)); // 设置样式的渐变色 灰色
    lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER); // 设置样式的颜色渐变方向为垂直方向
    lv_style_set_bg_grad_stop(&style,200);//渐变色起始点
    lv_obj_add_style(tileview, &style, 0);  // 给对象添加样式
    lv_obj_set_style_bg_opa(tileview, LV_OPA_0, LV_PART_SCROLLBAR | LV_STATE_DEFAULT);//滚动条透明
    lv_obj_set_style_bg_opa(tileview, LV_OPA_0, LV_PART_SCROLLBAR | LV_STATE_SCROLLED);


    /*****************************发光底部***************************************/
    lv_style_init(&round_style);
    lv_style_set_bg_color( &round_style, lv_color_hex(0x142626));//背景色 黑色
    round_button = lv_btn_create(tileview);
    lv_obj_set_size(round_button, 330,330);
    lv_obj_set_style_radius(round_button, 330, LV_PART_MAIN);
    lv_obj_align(round_button, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_shadow_spread(round_button,20,LV_PART_MAIN);      //设置样式四周偏移位置
    lv_obj_set_style_shadow_color(round_button,lv_palette_main(LV_PALETTE_BLUE),LV_PART_MAIN);  //设置样式阴影颜色
    lv_obj_set_style_shadow_width(round_button, 50, LV_PART_MAIN);
    lv_obj_add_style(round_button, &round_style, 0);  // 给对象添加样式
    lv_obj_clear_flag(round_button, LV_OBJ_FLAG_CLICKABLE);


/********************************上部圆形按钮控件**********************************/
    lv_style_init(&start_style);
    start_button = lv_btn_create(lv_scr_act());
    lv_obj_set_size(start_button, 260,260);
    lv_obj_set_style_radius(start_button, 260, LV_PART_MAIN);
    lv_obj_align(start_button, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_border_width(start_button, 5, LV_PART_MAIN);
    lv_obj_set_style_border_color(start_button, lv_color_hex(0x203B3B), LV_PART_MAIN);
    lv_style_set_bg_color(&start_style,lv_color_hex(0x2F4F4F));
    lv_obj_set_style_shadow_spread(start_button,10,LV_PART_MAIN);      //设置样式四周偏移位置
    lv_obj_set_style_shadow_color(start_button,lv_color_hex(0x000000),LV_PART_MAIN);  //设置样式阴影颜色
    lv_obj_set_style_shadow_width(start_button, 20, LV_PART_MAIN);
    lv_obj_add_style(start_button, &start_style, 0);  // 给对象添加样式
    lv_obj_clear_flag(start_button, LV_OBJ_FLAG_CLICKABLE);


/**********************************圆弧控件****************************************/
    arc=lv_arc_create(tileview);
    lv_arc_set_bg_angles(arc, 0, 300);
    lv_arc_set_range(arc,0,1000);
    lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
    lv_arc_set_rotation(arc, 120);
    lv_obj_set_size(arc, 320, 320);
    lv_obj_align(arc, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_arc_color(arc, lv_color_hex(0x0000), LV_PART_MAIN);
    lv_obj_set_style_arc_width(arc, 30, LV_PART_MAIN);
    lv_obj_set_style_arc_width(arc, 30, LV_PART_INDICATOR);
    lv_obj_add_event_cb(arc, arc_event_handler, LV_EVENT_VALUE_CHANGED, NULL);
/********************小按钮*******************************************************/

    small_button = lv_btn_create(start_button);
    lv_obj_set_size(small_button, 200,200);
    lv_obj_set_style_radius(small_button, 200, LV_PART_MAIN);
    lv_obj_align(small_button, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_border_width(small_button, 5, LV_PART_MAIN);
    lv_obj_set_style_border_color(small_button, lv_color_hex(0x696969), LV_PART_MAIN);
    lv_obj_set_style_bg_color(small_button, lv_color_hex(0x0000), LV_PART_MAIN );
    lv_obj_add_event_cb(small_button, button_event_handler, LV_EVENT_CLICKED,NULL);//按钮被点击事件
    /**小按钮标签*****/
    label = lv_label_create(small_button);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_42, 0);
    sprintf(str_lcd,"%2d g",0);
    lv_label_set_text(label, str_lcd);
    lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);

/**************运行时间************************************/
    timer = lv_timer_create(my_timer, 100,NULL);
    contorl_timer = lv_timer_create(my_contorl_timer, 2,NULL);
    /***********exti button *************************/
    exti_button = lv_btn_create(tileview);
    lv_obj_set_size(exti_button, 150,80);
    lv_obj_align(exti_button, LV_ALIGN_TOP_RIGHT, 0,0);
    lv_obj_set_style_bg_color(exti_button, lv_color_hex(0x2F4F4F), LV_PART_MAIN );
    lv_obj_add_event_cb(exti_button, exti_event_handler, LV_EVENT_CLICKED,NULL);//按钮被点击事件


    exti_label = lv_label_create(exti_button);
    lv_obj_set_style_text_font(exti_label, &lv_font_montserrat_42, 0);
    lv_label_set_text(exti_label, LV_SYMBOL_POWER); //显示POWER
    lv_obj_align(exti_label, LV_ALIGN_CENTER, 0, 0);
 /****************thread init********************************/
    hx711_init();
    int  status=pthread_create(&thread,NULL,thread_fuction,NULL);
    //pthread_create(&thread_ttt,NULL,thread_step,NULL);
    if(status !=0){ perror("thread creat failed:"); exit(0);}
    int ret = sem_init(&sem,0,0);
    if(ret ==-1) {perror("sem init failed:"); exit(0);}

}
static void arc_event_handler(lv_event_t * e)
{
    sprintf(str_lcd,"%d g",lv_arc_get_value(arc));
    lv_label_set_text(label, str_lcd);
}

static void button_event_handler(lv_event_t * e)
{
    if(control_fig==0){
        export=lv_arc_get_value(arc);
    }
    control_fig=!control_fig;
}

static void exti_event_handler(lv_event_t * e)
{
    exit(0);
}

uint16_t tim_100ms=0;
static void my_timer(lv_timer_t * timer)
{
    if(control_fig==1){
        if((hx711_value-export <thred_hx711)&&(hx711_value-export>-thred_hx711)){
            lv_label_set_text(label, LV_SYMBOL_OK); //显示OK
            lv_arc_set_value(arc,(int)hx711_value);
            lv_obj_set_style_bg_color(small_button, lv_color_hex(0x35DFDF), LV_PART_MAIN );
        }
        else{
            sprintf(str_lcd,"%d g",lv_arc_get_value(arc));
            lv_arc_set_value(arc,(int)hx711_value);
            lv_label_set_text(label, str_lcd);
            lv_obj_set_style_bg_color(small_button, lv_color_hex(0x0000), LV_PART_MAIN );
        }
    }
    else{
        sprintf(str_lcd,"%d g",lv_arc_get_value(arc));
        lv_label_set_text(label, str_lcd);
        lv_obj_set_style_bg_color(small_button, lv_color_hex(0x0000), LV_PART_MAIN );
    }
}

void uln2003(int step){
	switch(step){
		case 1:
		gpio_set(99,1);
		gpio_set(100,1);
		gpio_set(101,0);
		gpio_set(102,0);
		break;

		case 2:
                gpio_set(99,0);
                gpio_set(100,1);
                gpio_set(101,1);
                gpio_set(102,0);
                break;


		case 3:
                gpio_set(99,0);
                gpio_set(100,0);
                gpio_set(101,1);
                gpio_set(102,1);
                break;


		case 4:
                gpio_set(99,1);
                gpio_set(100,0);
                gpio_set(101,0);
                gpio_set(102,1);
                break;

		case 5:
                gpio_set(99,0);
                gpio_set(100,0);
                gpio_set(101,0);
                gpio_set(102,0);
                break;

	}
}
 static void  my_contorl_timer(lv_timer_t * timer){
	 static i=0;
   if(control_fig==1){      //contorl mode
   	if(hx711_value < export ) {
		i++;
		 uln2003(i);
		if(i==4) i=0;
	}	
	else if(hx711_value > export ) {
                 uln2003(i);
		 i--;
                if(i<=0) i=4;
        }

	else{
		uln2003(5);
	}
    }
    else{                    //not contorl mode
	uln2003(5);
    }
 }
